import urlConfig from './config.js';

export default ({url = "", method = "GET", data = {}, headers = {}}) => {
    return new Promise(function(resolve, reject) {
		let expires_time = uni.getStorageSync("expires_time");
		let token = uni.getStorageSync("token");		
	// 	if((expires_time-200 < new Date().toISOString().slice(0, 10) || token=="") && data.noCheck!="token" && data.iv==undefined){		
	// 　　　　 uni.clearStorageSync()
	// 		uni.redirectTo({ url: "/pages/user/login" })
	// 		return;
	// 	}
		if(token){
			headers['Authorization'] = `${ token }`;
		}
		uni.request({
			url: urlConfig + url,
			method,
			data: data,
			header: headers
		}).then(response => {
			const data = response ? response['data'] : [];
			const code = data ? data.code : 0;
			switch (code) {
				case "0": 
					return resolve(data)
					break;
				case "11":  //重新登录
	　　　　　　　　　uni.clearStorageSync()
					uni.redirectTo({
						url: "/pages/user/login"
					})
					break;
				case "14":  //重新登录
	　　　　　　　　　uni.clearStorageSync()
					uni.redirectTo({
						url: "/pages/user/login"
					})
					break;
				case "18":  //token不存在，跳转登录页面
	　　　　　　　　　uni.clearStorageSync()
					return resolve(data)
					break;
				case "406":  //绑定账号
					return resolve(data)
					break;
				default:
					uni.showToast({
						title: data.msg,
						icon: 'none'
					})
					return resolve(data)
					break;
			}
		}).catch(parmas => {
			uni.showToast({
				title: parmas.message,
				icon: 'none'
			})
			return reject()
	　　}) 
	});
} 